$(function () {
    var start = '';
    var end = '';
    var mark1 = '';
    var mark2 = '';
    /*百度地图初始化部分*/
    var map = new BMap.Map("allmap");
    var point = new BMap.Point(116.331398, 39.897445);
    map.centerAndZoom(point, 14);
    map.enableScrollWheelZoom(true); //允许滚动缩放
    map.addControl(new BMap.NavigationControl()); //添加地图平移缩放控件 
    map.addControl(new BMap.GeolocationControl()); //定位
    /*定位当前城市*/
    function myFun(result) {
        var cityName = result.name;
        map.setCenter(cityName);
    }
    var myCity = new BMap.LocalCity();
    myCity.get(myFun);

    /*导航效果*/
    $('.nav-item-content').each(function (i) {
        $(this).addClass("content-hide")
    });
    $("dd").each(function (i) {
        $(this).mouseenter(function () {
            $(this).addClass("ddhover");
        });
        $(this).mouseleave(function () {
            $(this).removeClass("ddhover");
        });
        $(this).click(function () {
            $("dd").removeClass("ddclick");
            $(this).addClass("ddclick");
        })
    });
    /*导航效果*/
    $('#btn1').click(function () {
        $('#item1').addClass("content-hide");
    })
    $('#btn2').click(function () {
        $('#item2').addClass("content-hide");
    })
    $('#btn3').click(function () {
        $('#item3').addClass("content-hide");
    })
    $('#btn4').click(function () {
        $('#item4').addClass("content-hide");
    })

    /*打开发布订单页*/
    $('.menu-release').click(function () {
        $('.nav-item-content').each(function (i) {
            $(this).addClass("content-hide")
        });
        $('#item1').removeClass("content-hide");

    });

    /*省市区三级联动*/
    $('#distpicker,#distpicker2').distpicker({
        autoSelect: false
    });

    /*提交订单*/
    $('#release-btn').click(function () {

        var sprovince = $('#sprovince').val().trim();
        var scity = $('#scity').val().trim();
        var sdistrict = $('#sdistrict').val().trim();
        var saddr = $('#saddr').val().trim();
        mark1 = sprovince + scity + sdistrict + saddr;
        var stime = ($('#sdate').val() + '-' + $('#stime').val()).trim();
        var info = $('textarea').val().trim();
        var eprovince = $('#eprovince').val().trim();
        var ecity = $('#ecity').val().trim();
        var edistrict = $('#edistrict').val().trim();
        var eaddr = $('#eaddr').val().trim();
        mark2 = eprovince + ecity + edistrict + eaddr;
        var etime = ($('#edate').val() + '-' + $('#etime').val()).trim();
        var price = $('#price').val();
        var timelimit = $('#timelimit').val();
        var cname = $('#cname').val().trim();
        var ctel = $('#ctel').val();
        if (sprovince == "" || scity == "" || sdistrict == "" || saddr == "") {
            alert("请将发货地址填写完整！");
            return;
        }
        if (stime == "" || etime == "" || price == "" || timelimit == "" || cname == "" || ctel == "") {
            alert("订单信息不能为空")
        }
        if (eprovince == "" || ecity == "" || edistrict == "" || eaddr == "") {
            alert("请将收货地址填写完整！");
            return;
        }
        $.ajax({
            url: 'api/allocate',
            type: 'post',
            data: {
                sprovince: sprovince, //发货地址-省
                scity: scity, //发货地址-市
                sdistrict: sdistrict, //发货地址-区
                saddr: saddr, //发货地址-详细地址
                stime: stime, //发货时间
                info: info, //货物描述
                eprovince: eprovince, //收货地址-省
                ecity: scity, //收货地址-市
                edistrict: edistrict, //收货地址-区
                eaddr: eaddr, //收货地址-详细地址
                etime: etime, //收货时间
                price: price, //运费
                timelimit: timelimit, //订单有效期
                cname: cname, //客户名
                ctel: ctel //客户电话
            },
            dataType: 'json',
            success: function (data) {
                alert(data.status);
            }
        })
    });

    /*打开竞价中的列表*/
    $('.menu-jjlist').click(function () {
        $('.nav-item-content').each(function (i) {
            $(this).addClass("content-hide")
        });
        $('#item2').removeClass("content-hide");
        $.ajax({
            url: 'api/JJorderlist',
            type: 'get',
            dataType: 'json',
            success: function (data) {
                var jtb = $('#Jtb').find('tbody');
                jtb.empty();
                $(data).each(function (index, obj) {
                    var jtr = $('<tr></tr>');
                    jtr.append('<td class="hide">' + obj.id + '</td>')
                        .append('<td>' + obj.cname + '</td>')
                        .append('<td>' + obj.sprovince + obj.scity + obj.sdistrict + '</td>')
                        .append('<td>' + obj.stime + '</td>')
                        .append('<td>' + obj.info + '</td>')
                        .append('<td>' + obj.price + '元</td>')
                        .append('<td>' + obj.releasetime + '</td>')
                        .append('<td>' + obj.lefttime + '分钟</td>')
                        .append('<td><a class="btn btn-primary btn-xs" data-toggle="modal" data-target="#detail">订单详情</a>&nbsp;&nbsp;<a class="btn btn-warning btn-xs" data-toggle="modal" data-target="#jjlist" id="jj-btn">竞价列表</a>');
                    jtr.find('td:nth-child(9) a:nth-child(1)').on('click', function () {
                        show_order_detail(obj.id)
                    });
                    jtr.find('td:nth-child(9) a:nth-child(2)').on('click', function () {
                        show_JJorder_list(obj.id)
                    });
                    jtr.appendTo(jtb);
                });
            }
        });
    });

    /*进行中的订单*/
    $('.menu-underway').click(function () {
        $('.nav-item-content').each(function (i) {
            $(this).addClass("content-hide")
        });
        $('#item3').removeClass("content-hide");
        $.ajax({
            url: 'api/orderlist',
            type: 'get',
            dataType: 'json',
            success: function (data) {
                var utb = $('#utb').find('tbody');
                utb.empty();
                $(data).each(function (index, obj) {
                    if (obj.flag == '1') {
                        var utr = $('<tr></tr>');
                        utr.append('<td class="hide">' + obj.id + '</td>')
                            .append('<td>' + obj.oid + '</td>')
                            .append('<td>' + obj.cname + '</td>')
                            .append('<td>' + obj.sprovince + obj.scity + obj.sdistrict + '</td>')
                            .append('<td>' + obj.stime + '</td>')
                            .append('<td>' + obj.eprovince + obj.ecity + obj.edistrict + '</td>')
                            .append('<td>' + obj.etime + '</td>')
                            .append('<td>' + obj.info + '</td>')
                            .append('<td>' + obj.carnum + '</td>')
                            .append('<td><a class="btn btn-primary btn-xs" data-toggle="modal" data-target="#detail">订单详情</a>&nbsp;&nbsp;<a class="btn btn-warning btn-xs" id="showroute">地图展示</a>&nbsp;&nbsp;<a class="btn btn-success btn-xs">完成</a>&nbsp;&nbsp;<a class="btn btn-danger btn-xs cancel-btn">取消</a>');
                        utr.find('td:nth-child(10) a:nth-child(1)').on('click', function () {
                            show_order_detail(obj.id)
                        });
                        utr.find('td:nth-child(10) a:nth-child(2)').on('click', function () {

                        });
                        utr.find('td:nth-child(10) a:nth-child(3)').on('click', function () {
                            confirm_order(obj.id)
                        });
                        utr.find('td:nth-child(10) a:nth-child(4)').on('click', function () {
                            cancel_order(obj.id)
                        });
                        utr.find('#showroute').on('click', function () {
                            $('#item3').addClass("content-hide");
                            start = obj.sprovince + obj.scity + obj.sdistrict + obj.saddr;
                            end = obj.eprovince + obj.ecity + obj.edistrict + obj.eaddr;
                            search(start, end);
                        });
                        utr.appendTo(utb);
                    }
                });
            }
        });
    });

    /*订单详情*/
    function show_order_detail(id) {
        var dialog = $('#detail');
        $.ajax({
            url: 'api/get-order-detail?id=' + id,
            type: 'get',
            dataType: 'json',
            success: function (data) {
                dialog.find('#detail-saddr').text(data.sprovince + data.scity + data.sdistrict + data.saddr);
                dialog.find('#detail-stime').text(data.stime);
                dialog.find('#detail-info').text(data.info);
                dialog.find('#detail-eaddr').text(data.eprovince + data.ecity + data.edistrict + data.eaddr);
                dialog.find('#detail-etime').text(data.etime);
                dialog.find('#detail-price').text(data.price);
                dialog.find('#detail-cname').text(data.cname);
                dialog.find('#detail-ctel').text(data.ctel);
            }
        })
    };

    /*竞价列表*/
    function show_JJorder_list(id) {
        var dialog = $('#jjlist');
        $.ajax({
            url: 'api/get-JJlist?id=' + id,
            type: 'get',
            dataType: 'json',
            success: function (data) {
                dialog.find('#list-saddr').text(data.sprovince + data.scity + data.sdistrict + data.saddr);
                dialog.find('#list-stime').text(data.stime);
                dialog.find('#list-info').text(data.info);
                dialog.find('#list-price').text(data.price);
                dialog.find('#list-release-time').text(data.releasetime);
                dialog.find('#list-left-time').text(data.lefttime + '分钟');
                //竞价列表排序
                dialog.find("button").click(function () {
                    var sort = $(this).attr('data-sort')
                    $.ajax({
                        url: 'api/carsort',
                        type: 'post',
                        data: {
                            sort: sort,
                            id: id
                        },
                        dataType: 'json',
                        success: function (data) {
                            flush_JJcartable(id);
                        }
                    })
                });
                var ctb = $('#clist').find('tbody');
                ctb.empty();
                $(data.carList).each(function (index) {
                    var ctr = $('<tr></tr>');
                    ctr.append('<td>' + data.carList[index].carnum + '</td>')
                        .append('<td>' + data.carList[index].credit + '</td>')
                        .append('<td>' + data.carList[index].dtel + '</td>')
                        .append('<td>' + data.carList[index].cprice + '</td>')
                        .append('<td><buttom class="btn btn-primary" id="select-btn">选择</buttom></td>');
                    ctr.find('td:nth-child(5) buttom').on('click', function () {
                        select_car(data.carList[index].cid);
                    });
                    ctr.appendTo(ctb);
                })

            }
        })
    };

    /*选择车辆*/
    function select_car(id) {
        if (confirm('是否确定下单？')) {
            $.ajax({
                url: 'api/select-car',
                type: 'post',
                data: {
                    cid: id
                },
                dataType: 'json',
                success: function (data) {
                    if (data.status == '选择成功') {
                        alert(data.status);
                        $('#jjlist').modal('hide');
                        flush_JJtable();
                    }

                }
            })
        }

    };

    /*刷新竞价车辆列表*/
    function flush_JJcartable(id) {
        var dialog = $('#jjlist');
        $.ajax({
            url: 'api/get-JJlist?id=' + id,
            type: 'get',
            dataType: 'json',
            success: function (data) {
                var ctb = $('#clist').find('tbody');
                ctb.empty();
                $(data.carList).each(function (index) {
                    var ctr = $('<tr></tr>');
                    ctr.append('<td>' + data.carList[index].carnum + '</td>')
                        .append('<td>' + data.carList[index].credit + '</td>')
                        .append('<td>' + data.carList[index].dtel + '</td>')
                        .append('<td>' + data.carList[index].cprice + '</td>')
                        .append('<td><buttom class="btn btn-primary" id="select-btn">选择</buttom></td>');
                    ctr.find('td:nth-child(5) buttom').on('click', function () {
                        select_car(data.carList[index].cid);
                    });
                    ctr.appendTo(ctb);
                })
            }
        })
    }

    /*刷新竞价表*/
    function flush_JJtable() {
        $.ajax({
            url: 'api/JJorderlist',
            type: 'get',
            dataType: 'json',
            success: function (data) {
                var jtb = $('#Jtb').find('tbody');
                jtb.empty();
                $(data).each(function (index, obj) {
                    var jtr = $('<tr></tr>');
                    jtr.append('<td class="hide">' + obj.id + '</td>')
                        .append('<td>' + obj.cname + '</td>')
                        .append('<td>' + obj.sprovince + obj.scity + obj.sdistrict + '</td>')
                        .append('<td>' + obj.stime + '</td>')
                        .append('<td>' + obj.info + '</td>')
                        .append('<td>' + obj.price + '元</td>')
                        .append('<td>' + obj.releasetime + '</td>')
                        .append('<td>' + obj.lefttime + '分钟</td>')
                        .append('<td><a class="btn btn-primary btn-xs" data-toggle="modal" data-target="#detail">订单详情</a>&nbsp;&nbsp;<a class="btn btn-warning btn-xs" data-toggle="modal" data-target="#jjlist" id="jj-btn">竞价列表</a>');
                    jtr.find('td:nth-child(9) a:nth-child(1)').on('click', function () {
                        show_order_detail(obj.id)
                    });
                    jtr.find('td:nth-child(9) a:nth-child(2)').on('click', function () {
                        show_JJorder_list(obj.id)
                    });
                    jtr.appendTo(jtb);
                });
            }
        });
    }

    /*取消订单*/
    function cancel_order(oid) {
        if (confirm('是否取消订单？')) {
            $.ajax({
                url: 'api/cancelorder',
                type: 'post',
                data: {
                    oid: oid
                },
                dataType: 'json',
                success: function (data) {
                    if (data.status == '取消成功') {
                        flush_JXtable();
                    }
                    alert(data.status);
                }
            })
        }
    };

    /*完成订单*/
    function confirm_order(oid) {
        if (confirm('确定完成订单？')) {
            $.ajax({
                url: 'api/confirmorder',
                type: 'post',
                data: {
                    oid: oid
                },
                dataType: 'json',
                success: function (data) {
                    if (data.status == '订单已完成') {
                        flush_JXtable();
                    }
                    alert(data.status);
                }
            })
        }
    };

    /*订单详情*/
    function show_order_detail(id) {
        var dialog = $('#detail');
        $.ajax({
            url: 'api/get-order-detail?id=' + id,
            type: 'get',
            dataType: 'json',
            success: function (data) {
                dialog.find('#detail-saddr').text(data.sprovince + data.scity + data.sdistrict + data.saddr);
                dialog.find('#detail-stime').text(data.stime);
                dialog.find('#detail-info').text(data.info);
                dialog.find('#detail-eaddr').text(data.eprovince + data.ecity + data.edistrict + data.eaddr);
                dialog.find('#detail-etime').text(data.etime);
                dialog.find('#detail-price').text(data.price);
                dialog.find('#detail-cname').text(data.cname);
                dialog.find('#detail-ctel').text(data.ctel);
            }
        })
    };

    /*刷新进行中的订单*/
    function flush_JXtable() {
        $.ajax({
            url: 'api/orderlist',
            type: 'get',
            dataType: 'json',
            success: function (data) {
                var utb = $('#utb').find('tbody');
                utb.empty();
                $(data).each(function (index, obj) {
                    if (obj.flag == '1') {
                        var utr = $('<tr></tr>');
                        utr.append('<td class="hide">' + obj.id + '</td>')
                            .append('<td>' + obj.oid + '</td>')
                            .append('<td>' + obj.cname + '</td>')
                            .append('<td>' + obj.sprovince + obj.scity + obj.sdistrict + '</td>')
                            .append('<td>' + obj.stime + '</td>')
                            .append('<td>' + obj.eprovince + obj.ecity + obj.edistrict + '</td>')
                            .append('<td>' + obj.etime + '</td>')
                            .append('<td>' + obj.info + '</td>')
                            .append('<td>' + obj.carnum + '</td>')
                            .append('<td><a class="btn btn-primary btn-xs" data-toggle="modal" data-target="#detail">订单详情</a>&nbsp;&nbsp;<a class="btn btn-warning btn-xs">地图展示</a>&nbsp;&nbsp;<a class="btn btn-success btn-xs">完成</a>&nbsp;&nbsp;<a class="btn btn-danger btn-xs cancel-btn">取消</a>');
                        utr.find('td:nth-child(10) a:nth-child(1)').on('click', function () {
                            show_order_detail(obj.id)
                        });
                        utr.find('td:nth-child(10) a:nth-child(2)').on('click', function () {
                            show_JJorder_list(obj.id)
                        });
                        utr.find('td:nth-child(10) a:nth-child(3)').on('click', function () {
                            confirm_order(obj.id)
                        });
                        utr.find('td:nth-child(10) a:nth-child(4)').on('click', function () {
                            cancel_order(obj.id)
                        });
                        utr.appendTo(utb);
                    }
                });
            }
        });
    };

    /*已完成订单*/
    $('.menu-completed').click(function () {
        $('.nav-item-content').each(function (i) {
            $(this).addClass("content-hide")
        });
        $('#item4').removeClass("content-hide");
        $.ajax({
            url: 'api/orderlist',
            type: 'get',
            dataType: 'json',
            success: function (data) {
                var ctb = $('#ctb').find('tbody');
                ctb.empty();
                $(data).each(function (index, obj) {
                    if (obj.flag == '0') {
                        var ctr = $('<tr></tr>');
                        ctr.append('<td class="hide">' + obj.id + '</td>')
                            .append('<td>' + obj.oid + '</td>')
                            .append('<td>' + obj.cname + '</td>')
                            .append('<td>' + obj.sprovince + obj.scity + obj.sdistrict + '</td>')
                            .append('<td>' + obj.stime + '</td>')
                            .append('<td>' + obj.eprovince + obj.ecity + obj.edistrict + '</td>')
                            .append('<td>' + obj.etime + '</td>')
                            .append('<td>' + obj.info + '</td>')
                            .append('<td>' + obj.carnum + '</td>')
                            .append('<td><a class="btn btn-primary btn-xs" data-toggle="modal" data-target="#detail">订单详情</a>');
                        ctr.find('td:nth-child(10) a').on('click', function () {
                            show_order_detail(obj.id)
                        });
                        ctr.appendTo(ctb);
                    }
                });
            }
        });
    });

    /* 注销*/
    $('#exit-link').click(function () {
        if (confirm('是否注销？')) {
            $.ajax({
                url: 'api/logout',
                type: 'post',
                dataType: 'json',
                success: function (data) {
                    alert(data.status);
                    if (data.status == '注销成功') {
                        window.location.href = "index.html";
                    }
                }
            });
        }
    });

    /*搜索订单*/
    $('#search-btn').click(function () {
        $('.nav-item-content').each(function (i) {
            $(this).addClass("content-hide")
        });
        var oid = $("#inp").val().trim();
        if (oid == '') {
            alert("搜索内容不能为空！")
        } else {
            $("#detail2").modal('show');
            var dialog = $('#detail2');
            $.ajax({
                url: 'api/ordersearch?oid=' + oid,
                type: 'get',
                dataType: 'json',
                success: function (data) {
                    start = data.sprovince + data.scity + data.sdistrict + data.saddr;
                    end = data.eprovince + data.ecity + data.edistrict + data.eaddr;
                    dialog.find('#detail-saddr').text(data.sprovince + data.scity + data.sdistrict + data.saddr);
                    dialog.find('#detail-stime').text(data.stime);
                    dialog.find('#detail-info').text(data.info);
                    dialog.find('#detail-eaddr').text(data.eprovince + data.ecity + data.edistrict + data.eaddr);
                    dialog.find('#detail-etime').text(data.etime);
                    dialog.find('#detail-zprice').text(data.zprice);
                    dialog.find('#detail-cname').text(data.cname);
                    dialog.find('#detail-ctel').text(data.ctel);
                    dialog.find('#detail-carnum').text(data.Carnum);
                    dialog.find('#detail-dtel').text(data.Dtel);
                }
            });
        }

    });

    /*规划路径*/
    $('#showRoute').click(function () {
        search(start, end);
    });

    function search(start, end) {
        map.clearOverlays();
        var driving = new BMap.DrivingRoute(map, {
            renderOptions: {
                map: map,
                autoViewport: true
            }
        });
        driving.search(start, end);
    };

    /*标记地点*/
    $('.btn-Mark1').click(function () {
        var sprovince = $('#sprovince').val().trim();
        var scity = $('#scity').val().trim();
        var sdistrict = $('#sdistrict').val().trim();
        var saddr = $('#saddr').val().trim();
        mark1 = sprovince + scity + sdistrict + saddr;
        // 创建地址解析器实例
        var myGeo = new BMap.Geocoder();
        // 将地址解析结果显示在地图上,并调整地图视野
        myGeo.getPoint(mark1, function (point) {
            if (point) {
                map.clearOverlays();
                map.centerAndZoom(point, 9);
                var marker = new BMap.Marker(point)
                map.addOverlay(marker);
                marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
            } else {
                alert("您选择地址没有解析到结果!");
            }
            $('#release-btn').click(function () {
                map.removeOverlay(marker);
            })
        });
    });

    $('.btn-Mark2').click(function () {
        var eprovince = $('#eprovince').val().trim();
        var ecity = $('#ecity').val().trim();
        var edistrict = $('#edistrict').val().trim();
        var eaddr = $('#eaddr').val().trim();
        mark2 = eprovince + ecity + edistrict + eaddr;
        // 创建地址解析器实例
        var myGeo = new BMap.Geocoder();
        // 将地址解析结果显示在地图上,并调整地图视野
        myGeo.getPoint(mark2, function (point) {
            if (point) {
                map.centerAndZoom(point, 9);
                var marker = new BMap.Marker(point)
                map.addOverlay(marker);
                marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
            } else {
                alert("您选择地址没有解析到结果!");
            }
            $('#release-btn').click(function () {
                map.removeOverlay(marker);
            })
        });
    });


})
